﻿// -----------------------------------------------------------------------
//  <copyright file="Startup.cs" company="OSharp开源团队">
//      Copyright (c) 2014-2020 OSharp. All rights reserved.
//  </copyright>
//  <site>http://www.osharp.org</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2020-03-26 21:52</last-date>
// -----------------------------------------------------------------------

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using OSharp.AspNetCore;
using OSharp.AspNetCore.Mvc;
using OSharp.AspNetCore.Routing;
using OSharp.AspNetCore.SignalR;
using OSharp.AutoMapper;
using OSharp.Log4Net;
using OSharp.Swagger;

using wyxuan.yoyu.Authorization;
using wyxuan.yoyu.Core.IM;
//using wyxuan.yoyu.Core.IM;
using wyxuan.yoyu.Identity;
using wyxuan.yoyu.Infos;
using wyxuan.yoyu.Systems;
using wyxuan.yoyu.Web.Pack;
using wyxuan.yoyu.Web.Startups;


namespace wyxuan.yoyu.Web
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {



            services.AddOSharp()
                .AddPack<Log4NetPack>()
                .AddPack<AutoMapperPack>()
                .AddPack<EndpointsPackEx>()
                .AddPack<SwaggerPack>()
                //.AddPack<RedisPack>()
                .AddPack<MvcPack>()
                .AddPack<AuthenticationPack>()
                .AddPack<FunctionAuthorizationPack>()
                .AddPack<DataAuthorizationPack>()
                .AddPack<MySqlDefaultDbContextMigrationPack>()
                .AddPack<AuditPack>()
                .AddPack<SignalRPack>()
                .AddPack<InfosPack>()
                .AddPack<IMPack>();

            //设置跨域访问
            services.AddCors(options =>
            {
                //登录用户使用
                options.AddPolicy("any", builder =>
                {
                    builder.WithOrigins(
                        "http://localhost:8080",
                        "http://localhost:8081",
                        "http://localhost:8082",
                        "null")
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                });
                //公开使用
                options.AddPolicy("all", builder =>
                {
                    builder.WithOrigins("*")
                    .AllowAnyMethod()
                    .AllowAnyHeader();
                });
            });

            //services.AddSignalR();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/#/500");
                app.UseHsts();
                //app.UseHttpsRedirection(); // 启用HTTPS
            }




            //app.UseMiddleware<HostHttpCryptoMiddleware>();
            //app.UseMiddleware<JsonNoFoundHandlerMiddleware>();
            app.UseMiddleware<JsonExceptionHandlerMiddleware>();
            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseCors("any");
            app.UseOSharp();
            app.UseWebSockets();

        }
    }
}